package com.example.floristshop;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.floristshop.database.Comment;
import com.example.floristshop.database.Flower;
import com.example.floristshop.database.User;
import com.example.floristshop.utils.DBFindUtil;
import com.example.floristshop.utils.SaveAsPicUtil;
import com.example.floristshop.utils.TimeUtil;
import com.example.floristshop.utils.UserImageUtil;
import com.github.clans.fab.FloatingActionButton;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class FlowerInfoActivity extends AppCompatActivity implements View.OnClickListener {
    Flower flower;
    List<Comment> commentList = new ArrayList<>();

    Toolbar toolbar;
    ImageView share;

    ImageView flower_image;
    TextView flower_name, flower_intro, flower_type, flower_origin;
    TextView comment_number;
    LinearLayout example_comment;
    CircleImageView example_user_image;
    TextView example_user_name, example_comment_date, example_comment_text;
    TextView no_comment_text;

    FloatingActionButton comment_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flower_info);

        // get data
        flower = DBFindUtil.findFlowerByFlowerId(getIntent().getLongExtra("flower_id", 1));

        // get view
        toolbar = (Toolbar)findViewById(R.id.flower_info_toolbar);
        share = (ImageView)findViewById(R.id.flower_info_share);

        flower_image = (ImageView)findViewById(R.id.flower_info_image);
        flower_name = (TextView)findViewById(R.id.flower_info_name);
        flower_intro = (TextView)findViewById(R.id.flower_info_intro);
        flower_type = (TextView)findViewById(R.id.flower_info_type);
        flower_origin = (TextView)findViewById(R.id.flower_info_origin);

        comment_number = (TextView)findViewById(R.id.flower_info_num_of_comments);
        example_comment = (LinearLayout) findViewById(R.id.example_comment);
        example_user_image = (CircleImageView)findViewById(R.id.example_user_image);
        example_user_name = (TextView)findViewById(R.id.example_user_name);
        example_comment_date = (TextView)findViewById(R.id.example_comment_content_date);
        example_comment_text = (TextView)findViewById(R.id.example_comment_content_text);
        no_comment_text = (TextView)findViewById(R.id.no_comment_text);

        comment_button = (FloatingActionButton)findViewById(R.id.comment_button);

        // set view
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        share.setOnClickListener(this::onClick);
        example_comment.setOnClickListener(this::onClick);
        comment_button.setOnClickListener(this::onClick);

        // inflate view
        flower_image.setImageResource(flower.getImage());
        flower_name.setText(flower.getName());
        flower_intro.setText(flower.getIntro());
        flower_type.setText(flower.getType());
        flower_origin.setText(flower.getOrigin());

        commentList = LitePal.where("flower_id==?", ""+flower.getFlower_id())
                .order("timestamp")
                .find(Comment.class);
        Collections.reverse(commentList);
        comment_number.setText(commentList.size()+"");
        if(commentList.size()==0){
            example_comment.setVisibility(View.GONE);
            no_comment_text.setVisibility(View.VISIBLE);
        }else {
            example_comment.setVisibility(View.VISIBLE);
            no_comment_text.setVisibility(View.GONE);
            Comment firstComment = commentList.get(0);
            User user = DBFindUtil.findUserByUserId(firstComment.getUser_id());
            example_user_image.setImageResource(UserImageUtil.id_to_src(user.getImage_id()));
            example_user_name.setText(user.getName());
            example_comment_date.setText(TimeUtil.timestamp_to_timeString(firstComment.getTimestamp()));
            example_comment_text.setText(firstComment.getText());
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.flower_info_share:
//                TODO: 实现分享功能
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SaveAsPicUtil.save(getApplicationContext(), "flower_share_pic"+System.currentTimeMillis()+".jpg", findViewById(R.id.flower_info_layout));
                        Toast.makeText(FlowerInfoActivity.this, "图片已保存到相册", Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.setTitle("分享");
                builder.setMessage("是否生成图片并保存？");
                builder.show();
                break;
            case R.id.example_comment:
                if(commentList.size()>0){
                    Intent intent = new Intent(this, CommentListActivity.class);
                    intent.putExtra("flower_id", flower.getFlower_id());
                    startActivity(intent);
                }
                break;
            case R.id.comment_button:
                Intent intent = new Intent(this, EditCommentActivity.class);
                intent.putExtra("flower_id", flower.getFlower_id());
                startActivityForResult(intent, 1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    commentList = LitePal.where("flower_id==?", ""+flower.getFlower_id())
                            .order("timestamp")
                            .find(Comment.class);
                    Collections.reverse(commentList);
                    Comment firstComment = commentList.get(0);
                    User user = DBFindUtil.findUserByUserId(firstComment.getUser_id());
                    example_user_image.setImageResource(UserImageUtil.id_to_src(user.getImage_id()));
                    example_user_name.setText(user.getName());
                    example_comment_date.setText(TimeUtil.timestamp_to_timeString(firstComment.getTimestamp()));
                    example_comment_text.setText(firstComment.getText());
                    comment_number.setText(commentList.size()+"");
                    if(commentList.size()==1){
                        example_comment.setVisibility(View.VISIBLE);
                        no_comment_text.setVisibility(View.GONE);
                    }
                }
                break;
            default:
        }
    }
}